package hotel.quartz.oversea.job.task;

import com.alibaba.fastjson.JSON;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.enums.MessageEnums;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.utils.SendUtils;
import hotel.quartz.oversea.config.service.RedisService;
import hotel.quartz.oversea.util.JobRestTemplateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class MeituanHotelDealNotifyGoodsTask {

    private Integer popNumber = 5;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisService redisService;

    private String timestamp;
    @Autowired
    private RestTemplate restTemplate;

    private void setTimestamp(String timestamp){
        this.timestamp = timestamp;
    }
    //@Async 标识这个方法异步执行，可返回void或Future
    @Async
    public void execute(String timestamp,String url,String paramsJon){
        setTimestamp(timestamp);

        log.info("Meituan Hotel Deal Notify Goods Job {} >>>>>>>>> {} 美团处理产品变更Task执行时间:{}", timestamp, DateUtil.getCurrTime());
        Map params = new HashMap();
        params.put("goods", paramsJon);
        Result response = JobRestTemplateUtils.post(url, JSON.toJSONString(params),restTemplate);
        log.info("Meituan Hotel Deal Notify Goods Job {} >>>>>>>>>  execute 美团处理产品变更Task  美团处理notify接口返回: {},{},{}", timestamp, JSON.toJSONString(params),hotel.base.oversea.utils.DateUtil.getCurrTime(),response);
        if(response.getCode()!= CommonConstants.SUCCESS){
            SendUtils.dingMsg("异步产品'变更'定时器-推送携程失败-请核实！", DateUtil.getCurrTime()+" MeituanHotelDealNotifyGoodsTask url="+url+" | response="+ JSON.toJSONString(response)+" | paramsJson="+ JSON.toJSONString(params), MessageEnums.genre.ROOM_CHANGE_FOCUS.getCode(),restTemplate);
            //log.error("异步产品'变更'定时器-推送携程失败-请核实！"+DateUtil.getCurrTime()+" MeituanHotelDealNotifyGoodsTask url="+url+" | response="+JSON.toJSONString(response)+" | paramsJson="+JSON.toJSONString(params));
        }
        log.info("Meituan Hotel Deal Notify Goods Job {} >>>>>>>>> 异步线程 美团处理产品变更Task 结束执行时间：{}",timestamp, DateUtil.getNowTime());
    }
}
